module.exports = {
	log_upload_save(event) {
		let _this = this;
		let { item, value, step = 0, callback } = event;

		if (step == 0) {
			let { click_type, view_tt, value_type, select_list, unit } = item;
			let config = {
				switch: {
					0: '关闭',
					1: '开启',
				},
				balanced: {
					0: '静态均衡',
					1: '充电均衡',
				},
			};
			uni.removeStorageSync('operation_msg');
			// 保存用户操作到缓存
			let operation_msg = '';
			if (click_type == 'switch') {
				operation_msg = `${view_tt}：${config['switch'][value]}`;
			} else if (click_type == 'select') {
				if (item.form_key == 'function_setting_balance_mode') {
					operation_msg = `${view_tt}：${config['balanced'][value]}`;
				} else {
					operation_msg = `${view_tt}：${select_list[value]['title']}`;
				}
			} else if (click_type == 'confirm') {
				operation_msg = `控制页面：${view_tt}`;
				if (value >= 0) {
					operation_msg += `_${value}`;
					if (_this.objJudge(unit)) {
						operation_msg += unit;
					}
				}
			} else if (click_type == 'mode_select') {
				operation_msg = `充电模式：${view_tt}`;
			} else {
				operation_msg = `${view_tt}：${value}`;
				if (_this.objJudge(unit)) {
					operation_msg += unit;
				}
			}
			uni.setStorageSync('operation_msg', operation_msg);
		} else if (step == 1) {
			let { user_phone } = _this.USER_MSG;
			let operation_msg = uni.getStorageSync('operation_msg');
			let dev_id = _this.BLUETOOTH_DEVICE_CONFIG.bluetooth_device_mac;

			if (!operation_msg || !dev_id || !user_phone) return;

			// console.log({
			//     operation_msg,
			//     dev_id,
			//     user_phone
			// })
			// 上传操作日志
			_this.com_getdata({
				url: '/bms/operation',
				data: {
					dev_id,
					user_phone,
					operation_msg,
				},
				data_fun: function (v) {
					if (v.state == 1) {
						uni.removeStorageSync('operation_msg');
						callback && callback();
					}
				},
			});
		}
	},
	async create_connect_judge(NEXT_DEVICEID, NEXT_MAC) {
		// 判断是否可以连接
		let _this = this;
		if (!_this.com_loading({ status: 1, type: 'none', throttle: true })) return;

		let { bluetooth_connect, bluetooth_deviceId } = _this.BLUETOOTH_DEVICE_CONFIG;
		_this.com_loading({ status: 0, type: 'none', throttle: true });
		// 当前已连接的话需要断开当前设备再连接下一个设备
		if (bluetooth_deviceId && bluetooth_connect) {
			_this.close_connect(bluetooth_deviceId, () => {
				setTimeout(() => {
					_this.create_connect(NEXT_DEVICEID, DEVICE);
				}, 300);
			});
			return;
		}
		if (NEXT_DEVICEID) {
			_this.create_connect(NEXT_DEVICEID, DEVICE);
		}
	},
};
